package com.iwhalecloud.bss.kite.cucc.service.scene.query;

import com.iwhalecloud.bss.kite.client.api.scene.query.IQuerySceneGiftOfferInstService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.dto.inst.InstAttrDTO;
import com.iwhalecloud.bss.kite.client.dto.inst.OfferInstDTO;
import com.iwhalecloud.bss.kite.client.dto.inst.OfferObjInstRelDTO;
import com.iwhalecloud.bss.kite.client.dto.inst.ProdInstDTO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.common.validate.annotation.KiteNotBlank;
import com.iwhalecloud.bss.kite.dataservice.bo.impl.QueryAccessProdInstBO;
import com.iwhalecloud.bss.kite.dataservice.bo.impl.QueryGiftOfferInstBO;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.dto.KiteInstRelDTO;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.service.util.CopyToDtoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author G
 * @since 2019/6/12
 */
@Service("querySceneGiftOfferInstService")
public class QuerySceneGiftOfferInstService extends QuerySceneOfferInstService implements IQuerySceneGiftOfferInstService {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private QueryGiftOfferInstBO queryGiftOfferInstBO;

    @Autowired
    private QueryAccessProdInstBO queryAccessProdInstBO;

    /***
     * 通过礼包规格获取实例对象；
     *
     * @param sceneInstId
     * @param giftOfferId
     * @param custId 客户id，分片字段，必填
     * @return
     */
    @Override
    @Validated
    public DataResult<OfferInstDTO> getGiftOfferInstByGiftId(@KiteNotBlank String sceneInstId,
        @KiteNotBlank String giftOfferId, @KiteNotBlank String custId) {
        KiteOfferInst kiteOfferInst = querySceneInstService.getGiftOfferInst(sceneInstId, giftOfferId);
        OfferInstDTO offerInstDTO = null;
        if (kiteOfferInst != null) {
            offerInstDTO = CopyToDtoUtil.packageOfferInstDTO(kiteOfferInst, null);
            // TODO 待确认 装箱
        }
        // else {
        // offerInstDTO = queryGiftOfferInstBO.getGiftOfferInstByOfferId(giftOfferId, custId);
        // checkAndPutOfferInst(offerInstDTO, queryGiftOfferInstBO);
        // }
        return ErrorConsts.SUCCESS.getResult(offerInstDTO);
    }

    /***
     * 获取礼包下所有接入产品成员
     *
     * @param sceneInstId
     * @param giftOfferInstId
     * @param custId 客户id，分片字段，必填
     * @return
     */
    @Override
    @Validated
    public DataResult<List<ProdInstDTO>> listAllProdInsts(@KiteNotBlank String sceneInstId,
        @KiteNotBlank String giftOfferInstId, @KiteNotBlank String custId) {
        List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInstsFromGiftOfferInst(custId,
            giftOfferInstId);
        List<ProdInstDTO> dtoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(kiteProdInstList)) {
            dtoList = CopyToDtoUtil.transProdInstDtoListForAccess(kiteProdInstList);
            // TODO 待确认 装箱
        }
        // else {
        // dtoList = queryGiftOfferInstBO.listAllProdInstsByGiftOfferInst(giftOfferInstId, custId);
        // checkAndPutProdInstList(dtoList, queryAccessProdInstBO);
        // }
        return ErrorConsts.SUCCESS.getResult(dtoList);
    }

    /***
     * 查询礼包套餐 -- 从缓存加载
     *
     * @param sceneInstId
     * @return
     */
    @Override
    public DataResult<OfferInstDTO> getGiftOfferInst(String sceneInstId) {
        KiteOfferInst kiteOfferInst = querySceneInstService.getGiftOfferInst(sceneInstId);
        OfferInstDTO offerInstDTO = null;
        if (kiteOfferInst != null) {
            offerInstDTO = CopyToDtoUtil.packageOfferInstDTO(kiteOfferInst, null);
            // TODO 待确认 装箱
        }
        return ErrorConsts.SUCCESS.getResult(offerInstDTO);
    }

    /***
     * 查询礼包套餐 -- 从缓存加载
     *
     * @param sceneInstId
     * @return
     */
    @Override
    @Validated
    public DataResult<List<OfferInstDTO>> getGiftOfferInstList(@KiteNotBlank String sceneInstId) {
        List<KiteOfferInst> kiteOfferInstList = querySceneInstService.listGiftOfferInsts(sceneInstId);
        List<OfferInstDTO> dtoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(kiteOfferInstList)) {
            dtoList = CopyToDtoUtil.transOfferInstDtoList(kiteOfferInstList);
            // TODO 待确认 装箱
        }
        return ErrorConsts.SUCCESS.getResult(dtoList);
    }

    @Override
    @Validated
    public DataResult<OfferInstDTO> getGiftOfferInstByAccNum(@KiteNotBlank String sceneInstId, @KiteNotBlank String accNum, @KiteNotBlank String custId) {
        KiteProdInst kiteProdInst = querySceneInstService.getProdInstByAccNum(sceneInstId, accNum);
        KiteOfferInst kiteOfferInst = querySceneInstService.getGiftOfferInstByProdInstId(custId, kiteProdInst.getProdInstId());
        OfferInstDTO offerInstDTO = null;
        if (kiteOfferInst != null) {
            offerInstDTO = CopyToDtoUtil.packageOfferInstDTO(kiteOfferInst, null);
        }
        return ErrorConsts.SUCCESS.getResult(offerInstDTO);
    }


    /***
     * 通过礼包销售品实例Id获取属性集合
     * @param sceneInstId
     * @param giftOfferInstId
     * @param custId
     * @return
     */
    @Override
    public DataResult<Map<String, List<InstAttrDTO>>> listMainOfferInstAttrsByGiftOfferInstId(String sceneInstId, String giftOfferInstId, String custId) {
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId);
        //获取礼包下面对应的主套餐的实例对象；
        List<KiteOfferInst> kiteMainOfferInsts = querySceneInstService.listMainOfferInstsFromGiftOfferInst(custId, giftOfferInstId);
        KiteOfferInst kiteOfferInst = querySceneInstService.getGiftOfferInst(sceneInstId);
        List<KiteOfferInst> promotions = querySceneInstService
            .listPromotionOfferInstsFromGiftOfferInst(custId, giftOfferInstId);
        kiteMainOfferInsts.addAll(promotions);
        kiteMainOfferInsts.add(kiteOfferInst);
        return ErrorConsts.SUCCESS.getResult(CopyToDtoUtil.transMapDataResult(sceneInst, kiteMainOfferInsts));
    }

    /***
     * 查询礼包下所有产品的明细；
     * @param sceneInstId
     * @param giftOfferInstId
     * @param custId
     * @return
     */
    @Override
    public DataResult<List<OfferObjInstRelDTO>> listOfferObjInstRelFromGiftOffer(String sceneInstId, String giftOfferInstId, String custId) {

        List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInstsFromGiftOfferInst(custId, giftOfferInstId);
        List<OfferObjInstRelDTO> returnList = new ArrayList();

        for (KiteProdInst prodInst : kiteProdInstList) {
            List<KiteInstRelDTO> instRels = querySceneInstService.listInstRelsByZInst(custId, InstType.PROD_INST, prodInst.getProdInstId(), Arrays.asList(InstType.MAIN_OFFER_INST, InstType.PROMOTION_OFFER_INST));
            List<KiteInstRelDTO> promotionOfferObjInstRels = instRels.stream().filter(rel -> Objects.equals(querySceneInstService.getOfferInst(custId, rel.getaInstId()).getInstType(), InstType.PROMOTION_OFFER_INST)).collect(Collectors.toList());
            KiteInstRelDTO instRel = null;

            if (KiteListUtils.isNotEmpty(promotionOfferObjInstRels)) {
                //针对第二条宽带特殊处理；
                instRel = promotionOfferObjInstRels.stream().filter(tempInstRel -> KiteStringUtils.equals(tempInstRel.getRoleId(), "78000002")).findFirst().orElse(null);
            }
            if (Objects.isNull(instRel)) {
                instRel = instRels.stream().filter(rel -> Objects.equals(querySceneInstService.getOfferInst(custId, rel.getaInstId()).getInstType(), InstType.PROMOTION_OFFER_INST)).findFirst().orElse(null);
            }
            if (Objects.isNull(instRel)) {
                instRel = instRels.stream().filter(rel -> Objects.equals(querySceneInstService.getOfferInst(custId, rel.getaInstId()).getInstType(), InstType.MAIN_OFFER_INST)).findFirst().orElse(null);
            }
            if (!Objects.isNull(instRel)) {
                returnList.add(CopyToDtoUtil.copyToOfferObjInstRelDTO(instRel, custId));
            }
        }
        return ErrorConsts.SUCCESS.getResult(returnList);
    }

    @Override
    public DataResult<KiteOfferInst> getGiftOfferInstByProdInstId(String custId, String prodInstId) {
        KiteOfferInst kiteOfferInst = querySceneInstService.getGiftOfferInstByProdInstId(custId, prodInstId);
        return ErrorConsts.SUCCESS.getResult(kiteOfferInst);
    }
}
